{% extends "base/micro.html" %}

{% block title %}
Node Add | SwarmOps - Docker管理平台
{% endblock %}

{% block container %}
<form class="form-horizontal" role="form" id="AddNodeForm">

  <div class="form-group">
    <label for="AddNodeIp" class="col-sm-12">Node Ip&nbsp;<span class="glyphicon glyphicon-star-empty"></span></label>
    <div class="col-sm-12">
      <input type="text" class="form-control" id="AddNodeIp" name="ip" placeholder="节点IP">
    </div>
  </div>

  <div class="radio">
    <label>
      <input type="radio" name="role" id="AddNodeRole" value="Worker" checked>作为工作节点加入集群
    </label>
  </div>
  <div class="radio">
    <label>
      <input type="radio" name="role" id="AddNodeRole" value="Manager">作为管理节点加入集群
    </label>
  </div>
  <br />

  <div class="form-group">
    <div class=" col-sm-12">
      <button type="button" id="AddNodeSubmit" onclick=AddNode() class="btn btn-success">添加节点</button>
    </div>
  </div>

</form>
{% endblock %}

{% block script %}
<script type="text/javascript">
function AddNode() {
    $.ajax({
        url: "{{ url_for('apis.core.node') }}",
        method: 'POST',
        datatype: 'json',
        data: $('#AddNodeForm').serialize(),
        beforeSend: function () {
            // 禁用按钮防止重复提交
            $("#AddNodeSubmit").attr({ disabled: "disabled" });
        },
        success: function(res) {
            console.log(res);
            if (res.success==true) {
                alert("节点加入集群成功!");
                parent.location.reload();
            } else {
                alert("节点加入集群失败，请重试提交或报告错误！\n错误原因是：" + res.msg);
                document.location.reload(); //重载当前页面
            }
        },
        error: function(res) {
            console.log(res);
            alert(res.statusText);
            document.location.reload(); //重载当前页面
        }
    });
}
</script>
{% endblock %}
